import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TestUserDAOImpl implements TestUserDAO {

    @Override
    public TestUser findByAccount(String account) {
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "SELECT * FROM Testuser WHERE account =?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                TestUser testUser = new TestUser();
                testUser.setAccount(resultSet.getString("account"));
                testUser.setTname(resultSet.getString("Tname"));
                testUser.setTsex(resultSet.getString("Tsex"));
                testUser.setTnumber(resultSet.getString("Tnumber"));
                testUser.setTbirth(resultSet.getDate("Tbirth"));
                testUser.setTprivacy(resultSet.getString("Tprivacy"));
                return testUser;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TestUser> getAllTestUsers() {
        List<TestUser> testUsers = new ArrayList<>();
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "SELECT * FROM Testuser";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                TestUser testUser = new TestUser();
                testUser.setAccount(resultSet.getString("account"));
                testUser.setTname(resultSet.getString("Tname"));
                testUser.setTsex(resultSet.getString("Tsex"));
                testUser.setTnumber(resultSet.getString("Tnumber"));
                testUser.setTbirth(resultSet.getDate("Tbirth"));
                testUser.setTprivacy(resultSet.getString("Tprivacy"));
                testUsers.add(testUser);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return testUsers;
    }

    @Override
    public void save(TestUser testUser) {
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "INSERT INTO Testuser (account, Tname, Tsex, Tnumber, Tbirth, Tprivacy) VALUES (?,?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, testUser.getAccount());
            preparedStatement.setString(2, testUser.getTname());
            preparedStatement.setString(3, testUser.getTsex());
            preparedStatement.setString(4, testUser.getTnumber());
            preparedStatement.setDate(5, new Date(testUser.getTbirth().getTime()));
            preparedStatement.setString(6, testUser.getTprivacy());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(TestUser testUser) {
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "UPDATE Testuser SET Tname =?, Tsex =?, Tnumber =?, Tbirth =?, Tprivacy =? WHERE account =?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, testUser.getTname());
            preparedStatement.setString(2, testUser.getTsex());
            preparedStatement.setString(3, testUser.getTnumber());
            preparedStatement.setDate(4, new Date(testUser.getTbirth().getTime()));
            preparedStatement.setString(5, testUser.getTprivacy());
            preparedStatement.setString(6, testUser.getAccount());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}